# 项目：light-edit
# 文件: 闵磊作页.py
# 作者：MING
# 日期：2025/5/20 19:52
# coding:utf-8

from selenium import webdriver
import time,random

from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.support.select import Select


def get_driver():

    driver = webdriver.Edge()
    driver.implicitly_wait(5)
    driver.maximize_window()
    return driver

def input(element, value):
    element.click()
    element.clear()
    element.send_keys(value)

def select_random_first(element):
    options_count = len(Select(element).options)
    random_index = random.randint(1, options_count-1)
    Select(element).select_by_index(random_index)

if __name__ == '__main__':

    driver = get_driver()
    driver.get('http://192.168.1.73:8080/YunDuanBoss/login/')
    uname = driver.find_element('name', 'userName')
    input(uname,'YDXA908')
    upass = driver.find_element('name', 'userPass')
    input(upass, 'YunDuan123')
    driver.find_element('css selector', '#form-login > div > div > div.modal-footer > button').click()
    time.sleep(3)
    driver.get('http://192.168.1.73:8080/YunDuanBoss/assets')
    driver.find_element('css selector', '#content > div.row.con-margin.con-body-con > div:nth-child(1) > div > button:nth-child(1)').click()
    # 随机选择设备名称
    assets_name = driver.find_element('css selector', '#addAss-form > div > div > div:nth-child(1) > div:nth-child(1) > select')
    select_random_first(assets_name)
    # 随机选择型号
    assets_type = driver.find_element('css selector', '#addAss-form > div > div > div:nth-child(1) > div:nth-child(2) > span:nth-child(2) > select')
    select_random_first(assets_type)
    # 输入合法的条码
    barcode = driver.find_element('css selector', '#addAss-form > div > div > div:nth-child(2) > div:nth-child(1) > input')
    input(barcode, '98865532101')
    price = driver.find_element('css selector', '#addAss-form > div > div > div:nth-child(2) > div.col-md-6.col-sm-6.form-group > input')
    input(price, '800')
    purchase_employee = driver.find_element('css selector', '#addAss-form > div > div > div:nth-child(3) > div:nth-child(1) > input.text.autoEmpId')
    input(purchase_employee, '张倩')
    time.sleep(2)
    ActionChains(driver).send_keys(Keys.DOWN).perform()
    time.sleep(2)
    ActionChains(driver).send_keys(Keys.ENTER).perform()
    time.sleep(2)
    # driver.find_element('xpath', '//*[@id="addAss-form"]/div/div/div[3]/div[1]/ul/li/a').click()
    # //*[@id="time"]/input
    driver.find_element('css selector', '#addAss-form > div:nth-child(1) > div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > input:nth-child(2)').click()
    time.sleep(2)
    driver.find_element('css selector', 'div.datetimepicker:nth-child(21) > div:nth-child(3) > table:nth-child(1) > tfoot:nth-child(3) > tr:nth-child(1) > th:nth-child(1)').click()
    owner = driver.find_element('css selector', '#addAss-form > div > div > div:nth-child(4) > div:nth-child(2) > select')
    select_random_first(owner)
    time.sleep(2)
    driver.find_element('css selector', '#addAss > div:nth-child(1) > div:nth-child(1) > div:nth-child(3) > button:nth-child(1)').click()
    time.sleep(3)